//
//  SavourViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SavourViewController.h"
#import "ItemZeroViewController.h"
#import "MyCell.h"
#import "AFNetworking.h"
#import "SDCycleScrollView.h"
#import "ItemFirstViewController.h"
#import "ItemSecondViewController.h"
#import "ItemThirdViewController.h"
#import "NightManager.h"
@interface SavourViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,SDCycleScrollViewDelegate,UIWebViewDelegate>
@property (nonatomic,retain)UICollectionView *collection;
@property (nonatomic,retain)SDCycleScrollView *cycle;
@property (nonatomic,retain)NSMutableArray *imgArr;

@end

@implementation SavourViewController


-(void)dealloc
{
    [_url release];
    [_collection release];
    [_cycle release];

    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.imgArr = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"course_master"],[UIImage imageNamed:@"course_steam"],[UIImage imageNamed:@"course_beak"],[UIImage imageNamed:@"course_homely"] ,nil];

    }
    
    return self;


}

#pragma mark 创建轮播图
-(void)createSDCycleScrollView
{
    
    NSArray *images = @[[UIImage imageNamed:@"first"],[UIImage imageNamed:@"second"],[UIImage imageNamed:@"third"]];
   
    
    self.cycle = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2-70) imagesGroup:images];
    
    self.cycle.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    
    self.cycle.delegate = self;
    
    self.cycle.dotColor = [UIColor whiteColor];
    
  

}
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self createSDCycleScrollView];
    
    [self createView];
    
}
#pragma mark 创建视图collectionView
-(void)createView
{
    

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    flow.itemSize = CGSizeMake(self.view.frame.size.width/2-30, self.view.frame.size.height/4-40);
    NSLog(@"sel is%g",self.view.frame.size.width/2-40);
    NSLog(@"%g",self.view.frame.size.height/4-30);
    flow.minimumLineSpacing =  1;
    
    flow.minimumInteritemSpacing = 1;
    
    flow.sectionInset = UIEdgeInsetsMake(15, 15, 5,15);
    
    self.collection = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flow];
    self.collection.backgroundColor = [ UIColor colorWithRed:245/255.0 green:247/255.0 blue:231/255.0 alpha:0.5];
    flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height/2-70);
    
    [self.view addSubview:self.collection];
    
    self.collection.delegate = self;
    
    self.collection.dataSource = self;
    
    [_collection release];
    [flow release];
    
    [self.collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;


}
#pragma mark cell赋值
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    
    
    cell.img.image = self.imgArr[indexPath.row];
    
    
    return cell;

}
#pragma mark 创建collectionViewHeader 并把轮播图加到上面
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        [head addSubview:self.cycle];
        return head;
    }else
    {
        return 0;
    
    }


}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ItemZeroViewController *zero = [[ItemZeroViewController alloc]init];
        
        [self.navigationController pushViewController:zero animated:YES];
        
        [zero release];
    }
    if (indexPath.row == 1) {
        ItemFirstViewController *first = [[ItemFirstViewController alloc]init];
        
        [self.navigationController pushViewController:first animated:YES];
        
        [first release];
    }
    if (indexPath.row == 2) {
        ItemSecondViewController *second = [[ItemSecondViewController alloc]init];
        
        [self.navigationController pushViewController:second animated:YES];
        
        [second release];
    }
    if (indexPath.row == 3) {
        ItemThirdViewController *third = [[ItemThirdViewController alloc]init];
        
        [self.navigationController pushViewController:third animated:YES];
        
        [third release];
    }


}
- (void)setNightDayModel{
    [NightManager setBackgroundColorWithView:self.view];
    
    [NightManager setBackgroundColorWithCollectionView:self.collection];
    
}
- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
    [NightManager setBackgroundColorWithCollectionView:self.collection];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
